package com.peanut.baby.mvp.mycontent;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.model.User;
import com.peanut.baby.mvp.BaseMvpActivity;
import com.peanut.baby.mvp.expert.live.ExpertLiveListFragment;
import com.peanut.baby.mvp.profile.ProfilePresenter;
import com.peanut.baby.mvp.profile.comments.ProfileReplysFragment;
import com.peanut.baby.mvp.profile.moments.ProfileMomentsFragment;
import com.peanut.baby.widget.TitleTabRadioButton;
import com.peanut.devlibrary.adapter.FragmentTabAdapter;
import com.peanut.devlibrary.widget.TitleLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by anonymouslystudio on 2017/8/2.
 */

public class MyContentActivity extends BaseMvpActivity implements RadioGroup.OnCheckedChangeListener,
        ViewPager.OnPageChangeListener,
        TitleLayout.OnTitleClickListener, MyContract.View {
    ArrayList<Fragment> fragments;
    FragmentTabAdapter adapter;

    @BindView(R.id.profile_radio_group)
    RadioGroup radioGroup;
    @BindView(R.id.profile_viewpager)
    ViewPager viewpager;
    @BindView(R.id.title)
    TitleLayout title;
    @BindView(R.id.profile_radio_bbs)
    TitleTabRadioButton radioBbs;
    @BindView(R.id.profile_radio_reply)
    TitleTabRadioButton radioReply;
    @BindView(R.id.profile_radio_course)
    TitleTabRadioButton profile_radio_course;
    @BindView(R.id.profile_radio_qa)
    TitleTabRadioButton profile_radio_qa;

    MyPresenter myPresenter;

    public static void start(Context ctx) {
        ctx.startActivity(new Intent(ctx, MyContentActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mycontent);
        ButterKnife.bind(this);
        myPresenter = new MyPresenter(this,this);
        initView();
    }

    private void initView() {
        title.setTitle("我的内容");
        title.setOnTitleClickedListener(this);

        viewpager.setOnPageChangeListener(this);
        radioGroup.setOnCheckedChangeListener(this);

        fragments = new ArrayList<>();
        fragments.add(ProfileMomentsFragment.newInstance(InitManager.getInstance().getUserId()));
        fragments.add(ProfileReplysFragment.newInstance(InitManager.getInstance().getUserId()));
        if (InitManager.getInstance().getUser().isNormalUser()) {
            fragments.add(CourseFragment.newInstance(InitManager.getInstance().getUserId()));
        } else {
            fragments.add(ExpertLiveListFragment.newInstance(InitManager.getInstance().getUserId()));
        }
        fragments.add(QaFragment.newInstance(InitManager.getInstance().getUserId()));
        adapter = new FragmentTabAdapter(getSupportFragmentManager(), fragments);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(3);

        if (InitManager.getInstance().getUser().isNormalUser()) {
            myPresenter.getUserInfo(InitManager.getInstance().getUserId());
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setCurrent(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.profile_radio_bbs:
                setCurrent(0);
                break;
            case R.id.profile_radio_reply:
                setCurrent(1);
                break;
            case R.id.profile_radio_course:
                setCurrent(2);
                break;
            case R.id.profile_radio_qa:
                setCurrent(3);
                break;

        }
    }

    private void setCurrent(int currentIndex) {
        RadioButton radioButton = (RadioButton) radioGroup.getChildAt(currentIndex);
        if (!radioButton.isChecked()) {
            radioButton.setChecked(true);
        }

        if (viewpager.getCurrentItem() != currentIndex) {
            viewpager.setCurrentItem(currentIndex);
        }
    }


    @Override
    public void onTitleBackClicked() {
        finish();
    }

    @Override
    public void onTitleRightClicked() {

    }

    @Override
    public void onTitleClicked() {

    }

    @Override
    public void onGetUserInfo(User user) {
        if (user != null) {
            radioBbs.setText("我的内容\n(" + user.postCount + ")");
            radioReply.setText("我的回复\n(" + user.replyCount + ")");
            profile_radio_course.setText("我的课程\n(" + user.liveCount + ")");
            profile_radio_qa.setText("我的问答\n(" + user.answerCount + ")");
        }
    }

    @Override
    public void onGetUserInfoFailed(String reason) {

    }
}
